home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Objects / accessobject.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  8KB  |  372 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. #ifdef SUPPORT_OBSOLETE_ACCESS /* This object type is no longer supported */
  33.  
  34. /* Access object implementation */
  35.  
  36. /* XXX TO DO LIST
  37.    - __init__ and __del__ (and all other similar methods)
  38.      should be usable even when private, not ignored
  39. */
  40.  
  41. #include "allobjects.h"
  42. #include "ceval.h"
  43. #include "structmember.h"
  44. #include "modsupport.h"        /* For getargs() etc. */
  45.  
  46. typedef struct {
  47.     OB_HEAD
  48.     object        *ac_value;
  49.     object        *ac_owner;
  50.     typeobject    *ac_type;
  51.     int        ac_mode;
  52. } accessobject;
  53.  
  54. /* Forward */
  55. static int typecheck PROTO((object *, typeobject *));
  56. static int ownercheck PROTO((object *, object *, int, int));
  57.  
  58. object *
  59. newaccessobject(value, owner, type, mode)
  60.     object *value;
  61.     object *owner;
  62.     typeobject *type;
  63.     int mode;
  64. {
  65.     accessobject *ap;
  66.     if (!typecheck(value, type)) {
  67.         err_setstr(AccessError,
  68.         "access: initial value has inappropriate type");
  69.         return NULL;
  70.     }
  71.     ap = NEWOBJ(accessobject, &Accesstype);
  72.     if (ap == NULL)
  73.         return NULL;
  74.     XINCREF(value);
  75.     ap->ac_value = value;
  76.     XINCREF(owner);
  77.     ap->ac_owner = owner;
  78.     XINCREF(type);
  79.     ap->ac_type = (typeobject *)type;
  80.     ap->ac_mode = mode;
  81.     return (object *)ap;
  82. }
  83.  
  84. object *
  85. cloneaccessobject(op)
  86.     object *op;
  87. {
  88.     register accessobject *ap;
  89.     if (!is_accessobject(op)) {
  90.         err_badcall();
  91.         return NULL;
  92.     }
  93.     ap = (accessobject *)op;
  94.     return newaccessobject(ap->ac_value, ap->ac_owner,
  95.                    ap->ac_type, ap->ac_mode);
  96. }
  97.  
  98. void
  99. setaccessowner(op, owner)
  100.     object *op;
  101.     object *owner;
  102. {
  103.     register accessobject *ap;
  104.     if (!is_accessobject(op))
  105.         return; /* XXX no error */
  106.     ap = (accessobject *)op;
  107.     XDECREF(ap->ac_owner);
  108.     XINCREF(owner);
  109.     ap->ac_owner = owner;
  110. }
  111.  
  112. int
  113. hasaccessvalue(op)
  114.     object *op;
  115. {    
  116.     if (!is_accessobject(op))
  117.         return 0;
  118.     return ((accessobject *)op)->ac_value != NULL;
  119. }
  120.  
  121. object *
  122. getaccessvalue(op, caller)
  123.     object *op;
  124.     object *caller;
  125. {
  126.     register accessobject *ap;
  127.     if (!is_accessobject(op)) {
  128.         err_badcall();
  129.         return NULL;
  130.     }
  131.     ap = (accessobject *)op;
  132.     
  133.     if (!ownercheck(caller, ap->ac_owner, AC_R, ap->ac_mode)) {
  134.         err_setstr(AccessError, "read access denied");
  135.         return NULL;
  136.     }
  137.     
  138.     if (ap->ac_value == NULL) {
  139.         err_setstr(AccessError, "no current value");
  140.         return NULL;
  141.     }
  142.     INCREF(ap->ac_value);
  143.     return ap->ac_value;
  144. }
  145.  
  146. int
  147. setaccessvalue(op, caller, value)
  148.     object *op;
  149.     object *caller;
  150.     object *value;
  151. {
  152.     register accessobject *ap;
  153.     if (!is_accessobject(op)) {
  154.         err_badcall();
  155.         return -1;
  156.     }
  157.     ap = (accessobject *)op;
  158.     
  159.     if (!ownercheck(caller, ap->ac_owner, AC_W, ap->ac_mode)) {
  160.         err_setstr(AccessError, "write access denied");
  161.         return -1;
  162.     }
  163.     
  164.     if (!typecheck(value, ap->ac_type)) {
  165.         err_setstr(AccessError, "assign value of inappropriate type");
  166.         return -1;
  167.     }
  168.     
  169.     if (value == NULL) { /* Delete it */
  170.         if (ap->ac_value == NULL) {
  171.             err_setstr(AccessError, "no current value");
  172.             return -1;
  173.         }
  174.         DECREF(ap->ac_value);
  175.         ap->ac_value = NULL;
  176.         return 0;
  177.     }
  178.     XDECREF(ap->ac_value);
  179.     INCREF(value);
  180.     ap->ac_value = value;
  181.     return 0;
  182. }
  183.  
  184. static int
  185. typecheck(value, type)
  186.     object *value;
  187.     typeobject *type;
  188. {
  189.     object *x;
  190.     if (value == NULL || type == NULL)
  191.         return 1; /* No check */
  192.     if (value->ob_type == type)
  193.         return 1; /* Exact match */
  194.     if (type == &Anynumbertype) {
  195.         if (value->ob_type->tp_as_number == NULL)
  196.             return 0;
  197.         if (!is_instanceobject(value))
  198.             return 1;
  199.         /* For instances, make sure it really looks like a number */
  200.         x = getattr(value, "__sub__");
  201.         if (x == NULL) {
  202.             err_clear();
  203.             return 0;
  204.         }
  205.         DECREF(x);
  206.         return 1;
  207.     }
  208.     if (type == &Anysequencetype) {
  209.         if (value->ob_type->tp_as_sequence == NULL)
  210.             return 0;
  211.         if (!is_instanceobject(value))
  212.             return 1;
  213.         /* For instances, make sure it really looks like a sequence */
  214.         x = getattr(value, "__getslice__");
  215.         if (x == NULL) {
  216.             err_clear();
  217.             return 0;
  218.         }
  219.         DECREF(x);
  220.         return 1;
  221.     }
  222.     if (type == &Anymappingtype) {
  223.         if (value->ob_type->tp_as_mapping == NULL)
  224.             return 0;
  225.         if (!is_instanceobject(value))
  226.             return 1;
  227.         /* For instances, make sure it really looks like a mapping */
  228.         x = getattr(value, "__getitem__");
  229.         if (x == NULL) {
  230.             err_clear();
  231.             return 0;
  232.         }
  233.         DECREF(x);
  234.         return 1;
  235.     }
  236.     return 0;
  237. }
  238.  
  239. static int
  240. isprivileged(caller)
  241.     object *caller;
  242. {
  243.     object *g;
  244.     static char privileged[] = "__privileged__";
  245.     if (caller != NULL && hasattr(caller, privileged))
  246.         return 1;
  247.     g = getglobals();
  248.     if (g != NULL && dictlookup(g, privileged))
  249.         return 1;
  250.     return 0;
  251. }
  252.  
  253. static int
  254. ownercheck(caller, owner, access, mode)
  255.     object *caller;
  256.     object *owner;
  257.     int access;
  258.     int mode;
  259. {
  260.     int mask = AC_PUBLIC;
  261.     if (caller == owner || isprivileged(caller))
  262.         mask |= AC_PRIVATE | AC_PROTECTED;
  263.     else if (caller != NULL && owner != NULL &&
  264.          is_classobject(owner) && is_classobject(caller) &&
  265.          (issubclass(caller, owner) ||
  266.           issubclass(owner, caller)))
  267.             mask |= AC_PROTECTED;
  268.     return access & mode & mask;
  269. }
  270.  
  271. /* Access methods */
  272.  
  273. static void
  274. access_dealloc(ap)
  275.     accessobject *ap;
  276. {
  277.     XDECREF(ap->ac_value);
  278.     XDECREF(ap->ac_owner);
  279.     XDECREF(ap->ac_type);
  280.     DEL(ap);
  281. }
  282.  
  283. #define OFF(x) offsetof(accessobject, x)
  284.  
  285. static struct memberlist access_memberlist[] = {
  286.     {"ac_value",    T_OBJECT,    OFF(ac_value)},
  287.     {"ac_owner",    T_OBJECT,    OFF(ac_owner)},
  288.     {"ac_type",    T_OBJECT,    OFF(ac_type)},
  289.     {"ac_mode",    T_INT,        OFF(ac_mode)},
  290.     {NULL}    /* Sentinel */
  291. };
  292.  
  293. static object *
  294. access_getattr(ap, name)
  295.     accessobject *ap;
  296.     char *name;
  297. {
  298.     return getmember((char *)ap, access_memberlist, name);
  299. }
  300.  
  301. static object *
  302. access_repr(ap)
  303.     accessobject *ap;
  304. {
  305.     char buf[300];
  306.     char buf2[20];
  307.     char *ownername;
  308.     typeobject *type = ap->ac_type;
  309.     if (is_classobject(ap->ac_owner)) {
  310.         ownername =
  311.             getstringvalue(((classobject *)ap->ac_owner)->cl_name);
  312.     }
  313.     else {
  314.         sprintf(buf2, "0x%lx", (long)ap->ac_owner);
  315.         ownername = buf2;
  316.     }
  317.     sprintf(buf,
  318.     "<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>",
  319.         (long)(ap->ac_value),
  320.         ownername,
  321.         type ? type->tp_name : "-",
  322.         ap->ac_mode);
  323.     return newstringobject(buf);
  324. }
  325.  
  326. typeobject Accesstype = {
  327.     OB_HEAD_INIT(&Typetype)
  328.     0,            /*ob_size*/
  329.     "access",        /*tp_name*/
  330.     sizeof(accessobject),    /*tp_size*/
  331.     0,            /*tp_itemsize*/
  332.     /* methods */
  333.     (destructor)access_dealloc, /*tp_dealloc*/
  334.     0,            /*tp_print*/
  335.     (getattrfunc)access_getattr, /*tp_getattr*/
  336.     0,            /*tp_setattr*/
  337.     0,            /*tp_compare*/
  338.     (reprfunc)access_repr, /*tp_repr*/
  339.     0,            /*tp_as_number*/
  340.     0,            /*tp_as_sequence*/
  341.     0,            /*tp_as_mapping*/
  342.     0,            /*tp_hash*/
  343. };
  344.  
  345.  
  346. /* Pseudo type objects to indicate collections of types */
  347.  
  348. /* XXX This should be replaced by a more general "subclassing"
  349.    XXX mechanism for type objects... */
  350.  
  351. typeobject Anynumbertype = {
  352.     OB_HEAD_INIT(&Typetype)
  353.     0,            /*ob_size*/
  354.     "*number*",        /*tp_name*/
  355. };
  356.  
  357. /* XXX Should really distinguish mutable and immutable sequences as well */
  358.  
  359. typeobject Anysequencetype = {
  360.     OB_HEAD_INIT(&Typetype)
  361.     0,            /*ob_size*/
  362.     "*sequence*",        /*tp_name*/
  363. };
  364.  
  365. typeobject Anymappingtype = {
  366.     OB_HEAD_INIT(&Typetype)
  367.     0,            /*ob_size*/
  368.     "*mapping*",        /*tp_name*/
  369. };
  370.  
  371. #endif /* SUPPORT_OBSOLETE_ACCESS */
  372.